/*************************************
 * Created by WebStorm
 * QQ邮箱：2891044172@qq.com  手机号：15981761402
 * Created Time :2024/2/28 18:45
 * Created Time :2024/3/1 22:22
 Desc :
 **************************************/
interface DateFns {
    today: () => string,
    yesterday: () => string,
    last7Days: () => Array<string>
    last30Days: () => Array<string>
    currentMonth: () => Array<string>
    currentYear: () => Array<string>
    ttConversion: (value: number) => string

}

const DateFns: DateFns = {
    today: (): string => {
        return formatDate(new Date());
    },
    yesterday: (): string => {
        const yesterday = new Date();
        yesterday.setDate(yesterday.getDate() - 1);
        return formatDate(yesterday);
    },
    last7Days: (): Array<string> => {
        const endDate = new Date();
        const startDate = new Date();
        startDate.setDate(endDate.getDate() - 6); // 获取7天前的日期（包括今天在内，所以是减6）
        return [formatDate(startDate), formatDate(endDate)];
    },
    last30Days: (): Array<string> => {
        const endDate = new Date();
        const startDate = new Date();
        startDate.setDate(endDate.getDate() - 29); // 获取30天前的日期（包括今天在内，所以是减6）
        return [formatDate(startDate), formatDate(endDate)];
    },
    currentMonth: (): Array<string> => {
        const today = new Date();
        const startDate = new Date(today.getFullYear(), today.getMonth(), 1); // 本月的第一天
        const endDate = new Date(today.getFullYear(), today.getMonth() + 1, 0); // 本月的最后一天
        return [formatDate(startDate), formatDate(endDate)];
    },
    currentYear: (): Array<string> => {
        const today = new Date();
        const startDate = new Date(today.getFullYear(), 0, 1); // 本年的第一天
        const endDate = new Date(today.getFullYear(), 11, 31); // 本年的最后一天
        return [formatDate(startDate), formatDate(endDate)];
    },
    ttConversion: (value: number) => {
        const date = new Date(value);
        // 调整时区偏差
        const userTimezoneOffset = date.getTimezoneOffset() * 60000;
        const adjustedDate = new Date(date.getTime() - userTimezoneOffset);
        return adjustedDate.toISOString().split('T')[0];
    }

}
const formatDate = (date: Date): string => {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    return `${year}-${month}-${day}`;
}

export default DateFns;